﻿using System.IO;
using System.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace C12_Ex02_Shaked_032675316_Itay_032511057
{
    public partial class DownloadGalleries : UserControl
    {
        public static readonly string r_DefaultDownloadPath = System.Environment.GetEnvironmentVariable("HOMEPATH") + Path.DirectorySeparatorChar + "Desktop";

        private Dictionary<string, Album> m_AlbumsToDownload;
        private FacebookObjectCollection<Album> m_Albums;

        public DownloadGalleries()
        {
            InitializeComponent();
            m_AlbumsToDownload = new Dictionary<string, Album>();
            TextBoxDirectory.Text = r_DefaultDownloadPath;
        }

        private string m_UserName;

        public string UserName
        {
            get
            {
                return m_UserName;
            }

            set
            {
                m_UserName = value;
                TextBoxDirectory.Text = r_DefaultDownloadPath + "\\" + UserName;
            }
        }

        public FacebookObjectCollection<Album> Albums
        {
            get { return m_Albums; }
            set
            {
                m_Albums = value;
                reloadPictures();
            }
        }

        private void reloadPictures()
        {
            CheckedListBox1.Items.Clear();
            foreach (Album album in m_Albums)
            {
                CheckedListBox1.Items.Add(album.Name);
                m_AlbumsToDownload[album.Name] = album;
            }
        }

        private void buttonDownloadGalleries_Click(object sender, EventArgs e)
        {
            foreach (string item in CheckedListBox1.CheckedItems)
            {
                Album albumToDownload = m_AlbumsToDownload[item];
                foreach (Photo photo in albumToDownload.Photos)
                {
                    IFaceBookGalleriesDownloadHandler downloaderHandler = AsyncFaceBookGalleriesDownloadHandler.Instance;
                    downloaderHandler.DownloadImage(photo.URL, albumToDownload.Name, TextBoxDirectory.Text);
                }
            }
        }
    }
}
